//
//  WHConfirmView.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/8.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHConfirmView.h"

@interface WHConfirmView ()

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIView *backContentView;

@property (nonatomic, strong) UILabel *numberLabel;

@property (nonatomic, strong) UIView *numberGapView;

@property (nonatomic, strong) UILabel *phoneLabel;


@property (nonatomic, strong) UIView *phoneGapView;

@property (nonatomic, strong) UIButton *confirmBtn;

@end

@implementation WHConfirmView

- (void)dealloc {
    [self ungisterNOtification];
    DLog(@"WHTimeInputView");
}

- (void)registerNotification {
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)ungisterNOtification {
    [kNotificationCenter removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyboardWillChangeFrame:(NSNotification *)aNSNotification {
    NSValue *keyBoardEndBounds = [[aNSNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect endRect = [keyBoardEndBounds CGRectValue];
    
    [UIView animateWithDuration:0.25f animations:^{
        if (endRect.origin.y - KScreenHeight/2 < (_backContentView.height/2)+40) {
            _backContentView.centerY = endRect.origin.y - 40 - _backContentView.height/2;
        } else {
            _backContentView.centerY = KScreenHeight/2;
        }
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUpConfirmView];
        [self registerNotification];
    }
    return self;
}

- (void)setUpConfirmView {
    
    [self addSubview:({
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        _backView;
    })];
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTap)];
    [_backView addGestureRecognizer:tapGes];
    
    [self addSubview:({
        _backContentView = [[UIView alloc] init];
        _backContentView.backgroundColor = KWhiteColor;
        _backContentView.frame = CGRectMake(0, 0, KScreenWidth-2*kRealwidth(15), kRealwidth(190));
        _backContentView;
    })];
    
    [_backContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backView.mas_left).offset(kRealwidth(15));
        make.right.mas_equalTo(_backView.mas_right).offset(-kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(180));
        make.centerY.mas_equalTo(0);
    }];
    
    [_backContentView addSubview:({
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.textColor = CMDarkOrangeColor;
        _numberLabel.font = SYSTEMFONT(kRealwidth(15));
        _numberLabel.text = @"运单号";
        _numberLabel;
    })];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backContentView.mas_left).offset(kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(15));
        make.top.mas_equalTo(_backContentView.mas_top).offset(kRealwidth(25));
    }];
    
    [_backContentView addSubview:({
        _numberField = [[UITextField alloc] init];
        _numberField.textColor = CFontTitleBlackColor;
        _numberField.font = SYSTEMFONT(kRealwidth(15));
        _numberField;
    })];
    [_numberField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backContentView.mas_left).offset(kRealwidth(15));
        make.right.mas_equalTo(_backContentView.mas_right).offset(-kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(20));
        make.top.mas_equalTo(_numberLabel.mas_bottom).offset(kRealwidth(10));
    }];
    
    [_backContentView addSubview:({
        _numberGapView = [[UIView alloc] init];
        _numberGapView.backgroundColor = CLineColor;
        _numberGapView;
    })];
    [_numberGapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backContentView.mas_left).offset(kRealwidth(15));
        make.right.mas_equalTo(_backContentView.mas_right).offset(-kRealwidth(15));
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(_numberField.mas_bottom);
    }];
    
    [_backContentView addSubview:({
        _phoneLabel = [[UILabel alloc] init];
        _phoneLabel.textColor = CMDarkOrangeColor;
        _phoneLabel.font = SYSTEMFONT(kRealwidth(15));
        _phoneLabel.text = @"手机号";
        _phoneLabel;
    })];
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backContentView.mas_left).offset(kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(15));
        make.top.mas_equalTo(_numberGapView.mas_bottom).offset(kRealwidth(10));
    }];
    
    [_backContentView addSubview:({
        _phoneField = [[UITextField alloc] init];
        _phoneField.textColor = CFontTitleBlackColor;
        _phoneField.font = SYSTEMFONT(kRealwidth(15));
        _phoneField;
    })];
    [_phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backContentView.mas_left).offset(kRealwidth(15));
        make.right.mas_equalTo(_backContentView.mas_right).offset(-kRealwidth(15));
        make.height.mas_equalTo(kRealwidth(20));
        make.top.mas_equalTo(_phoneLabel.mas_bottom).offset(kRealwidth(10));
    }];
    
    [_backContentView addSubview:({
        _phoneGapView = [[UIView alloc] init];
        _phoneGapView.backgroundColor = CLineColor;
        _phoneGapView;
    })];
    [_phoneGapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backContentView.mas_left).offset(kRealwidth(15));
        make.right.mas_equalTo(_backContentView.mas_right).offset(-kRealwidth(15));
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(_phoneField.mas_bottom);
    }];
    
    [_backContentView addSubview:({
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"确定" attributes:@{NSFontAttributeName:SYSTEMFONT(kRealwidth(16)), NSForegroundColorAttributeName:KWhiteColor}];
        [_confirmBtn setAttributedTitle:attStr forState:UIControlStateNormal];
        _confirmBtn.backgroundColor = CMDarkOrangeColor;
        _confirmBtn;
    })];
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_backContentView);
        make.height.mas_equalTo(kRealwidth(45));
    }];
    [_confirmBtn addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    
    kWeakSelf(self);
    _numberField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(40).set_regularStr(@"^[a-zA-Z0-9]{0,40}$").set_textChanged(^(id obj) {
        if (weakself.numberChange) {
            weakself.numberChange([obj valueForKey:@"text"]);
        }
    });
    
    _phoneField.keyboardType = UIKeyboardTypeNumberPad;
    _phoneField.yb_inputCP = YBInputControlProfile.creat.set_maxLength(11).set_regularStr(@"^[0-9]{0,11}+$").set_textChanged(^(id obj) {
        if (weakself.phoneChange) {
            weakself.phoneChange([obj valueForKey:@"text"]);
        }
    });
}

- (void)confirm:(UIButton *)button {
    [self endEditing:YES];
    if (![self.numberField.text isEqualToString:@""] && ![self.phoneField.text isEqualToString:@""]) {
        if (self.okClicked) {
            self.okClicked();
        }
        [self removeSelf];
    }
}

- (void)removeSelf {
    [UIView animateWithDuration:.3f animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)backTap {
    [self removeSelf];
    if (self.cancelClicked) {
        self.cancelClicked();
    }
}

@end
